package it.bnl.layar.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import it.bnl.layar.data.Hotspot;

public class HotspotData {
	// poiType enumeration
	public static final int FOLLOW_ME = 1;
	public static final int BREADCRUMB = 2;
	public static final int STATIC = 3;
	public static final int FIXED = 4;
	public static final float earthRadius = 6372.795477598f;
	
    private String id;
	private String title;
	private float lat;
	private float lon;
	private List<ActionData> actions;
	private int dimension;
	private int alt;
	private int relativeAlt;
    private ObjectData object;
    private TransformData transform;
    private String userId;
    private Date expDate;
    private int poiType;
    private boolean active;
    private int type;
    private String attribution;
    private String line2;
    private String line3;
    private String line4;
    private String imageURL;
    
    
    public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getDimension() {
		return dimension;
	}

	public void setDimension(int dimension) {
		this.dimension = dimension;
	}

	public int getAlt() {
		return alt;
	}

	public void setAlt(int alt) {
		this.alt = alt;
	}

	public ObjectData getObject() {
		return object;
	}

	public void setObject(ObjectData object) {
		this.object = object;
	}

	public TransformData getTransform() {
		return transform;
	}

	public void setTransform(TransformData transform) {
		this.transform = transform;
	}
	
	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public float getLat() {
		return lat;
	}
	public void setLat(float lat) {
		this.lat = lat;
	}
	public float getLon() {
		return lon;
	}
	public void setLon(float lon) {
		this.lon = lon;
	}
	public List<ActionData> getActions() {
		return actions;
	}
	public void setActions(List<ActionData> actions) {
		this.actions = actions;
	}
    public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}
    public Date getExpDate() {
		return expDate;
	}

	public void setExpDate(Date expDate) {
		this.expDate = expDate;
	}

	public int getPoiType() {
		return poiType;
	}

	public void setPoiType(int poiType) {
		this.poiType = poiType;
	}
    public int getRelativeAlt() {
		return relativeAlt;
	}

	public void setRelativeAlt(int relativeAlt) {
		this.relativeAlt = relativeAlt;
	}

	
	public HotspotData() {
		actions = new ArrayList<ActionData>();
	}
	
	// Create a Hotspot to be returned to the Layar browser
	public Hotspot createHotSpot(float currLat, float currLon) {
		Hotspot hs = new Hotspot();
		hs.setActions(actions);
		hs.setId(id);
		hs.setLat(formatCoord(lat));
		hs.setLon(formatCoord(lon));
		hs.setDistance(calculateDistance(currLat, currLon, getLat(), getLon()));
		hs.setTitle(title);
		hs.setType(type);
		hs.setDimension(dimension);
		hs.setObject(object);
		hs.setTransform(transform);
		hs.setAlt(alt);
		hs.setRelativeAlt(relativeAlt);
		hs.setAttribution(attribution);
		hs.setLine2(line2);
		hs.setLine3(line3);
		hs.setLine4(line4);
		hs.setImageURL(imageURL);
		return hs;
	}
		
	// Convert a float coordinate in a string without decimal point
	// The decimal part is limited to 6 digits
	// Example: 45.1234567f = "45123456"
	private String formatCoord(float coordf) {
		int cint = new Float(coordf * 1e6f).intValue();
		String coord = Integer.toString(cint);
		return coord;
	}
	
	// Calculate the distance in meters between two locations
	static public float calculateDistance(float lat1, float lon1, float lat2, float lon2) {
		float lat1r = (float)Math.toRadians(lat1);
		float lon1r = (float)Math.toRadians(lon1);
		float lat2r = (float)Math.toRadians(lat2);
		float lon2r = (float)Math.toRadians(lon2);
		float dlon = Math.abs(lon1r - lon2r);
		float P = (float)Math.acos(Math.sin(lat1r)*Math.sin(lat2r)+Math.cos(lat1r)*Math.cos(lat2r)*Math.cos(dlon));
		return (P * earthRadius)*1000f;
	}

	public String getAttribution() {
		return attribution;
	}

	public void setAttribution(String attribution) {
		this.attribution = attribution;
	}

	public String getLine2() {
		return line2;
	}

	public void setLine2(String line2) {
		this.line2 = line2;
	}

	public String getLine3() {
		return line3;
	}

	public void setLine3(String line3) {
		this.line3 = line3;
	}

	public String getLine4() {
		return line4;
	}

	public void setLine4(String line4) {
		this.line4 = line4;
	}

	public String getImageURL() {
		return imageURL;
	}

	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}
}
